package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerController")
public class CrawlerController extends CmiEmaController {

	@RequestMapping("/listCrawlers")
	public ModelAndView listCrawlers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerPaginationService.updatePagination(request, pagination);
		Set<Crawler> crawlers = crawlerPaginationService.getPaginationCrawlers(pagination);

		mav.addObject("crawlers", crawlers);
		mav.addObject("entityName", "crawler");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawler/listCrawlers.jsp");
		return mav;
	}

	public String listCrawlers() {
		return "redirect:/listCrawlers";
	}

	@RequestMapping("/viewCrawler")
	public ModelAndView viewCrawler(@RequestParam Integer crawlerId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawler", crawlerDAO.findCrawlerByPrimaryKey(crawlerId));
		mav.setViewName("crawler/viewCrawler.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlers")
	public ModelAndView confirmDeleteCrawlers(
			@RequestParam(value = "deleteCrawlersIds", required = true) Set<Integer> deleteCrawlersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawler> deleteCrawlers = crawlerDAO.findCrawlersByIds(deleteCrawlersIds);
		Set<Crawler> referencedCrawlers = crawlerService.loadReferenceCrawlers(deleteCrawlers);
		boolean hasReferenced;
		if (referencedCrawlers.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlers);
			hasReferenced = false;
		} else {
			deleteCrawlers.removeAll(referencedCrawlers);
			mav.addObject("referencedEntitys", referencedCrawlers);
			mav.addObject("deleteEntitys", deleteCrawlers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawler");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlers");
		return mav;
	}

	@RequestMapping("/deleteCrawler")
	public String deleteCrawler(@RequestParam Integer crawlerId) {
		Crawler crawler = crawlerDAO.findCrawlerByPrimaryKey(crawlerId);
		crawlerService.deleteCrawler(crawler);
		return "forward:/listCrawlers";
	}

	@RequestMapping("/confirmDeleteCrawler")
	public ModelAndView confirmDeleteCrawler(@RequestParam Integer crawlerId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawler", crawlerDAO.findCrawlerByPrimaryKey(crawlerId));

		mav.setViewName("crawler/deleteCrawler.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlers")
	public String deleteCrawlers(
			@RequestParam(value = "deleteCrawlersId", required = true) Set<Integer> deleteCrawlersId) {
		crawlerService.deleteCrawlers(deleteCrawlersId);
		return "forward:/listCrawlers";
	}

	@RequestMapping("/editCrawler")
	public ModelAndView editCrawler(@RequestParam Integer crawlerId) {

		ModelAndView mav = new ModelAndView();

		Crawler crawler = crawlerDAO.findCrawlerByPrimaryKey(crawlerId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawler", crawler);

		// 把所有的引用crawler对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerService.loadCrawlerObjectReferencedSet(crawler));

		// 将编辑页面crawler添加到mav中
		mav.setViewName("crawler/editCrawler.jsp");
		return mav;
	}

	@RequestMapping("/newCrawler")
	public ModelAndView newCrawler() {
		ModelAndView mav = new ModelAndView();
		Crawler crawler = new Crawler();

		mav.addObject("crawler", crawler);

		// 把所有的引用crawler对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerService.loadCrawlerObjectReferencedSet(crawler));

		mav.addObject("newFlag", true);
		mav.setViewName("crawler/editCrawler.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawler")
	public ModelAndView saveCrawler(@ModelAttribute Crawler crawler) {

		ModelAndView mav = new ModelAndView();

		Crawler existingCrawler = CmiHibernateProxyUtil
				.deproxy(crawlerDAO.findCrawlerByPhysicalPrimaryKey(crawler.getName()));

		// 新建记录
		if (crawler.getId() == null) {
			if (crawler.equals(existingCrawler)) {

				mav.addAllObjects(crawlerService.loadCrawlerClassReferencedSet(crawler));
				// mav = editContinue(mav, crawler);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawler/editCrawler.jsp");
			} else {
				crawlerService.saveCrawler(crawler);
				mav.setViewName("forward:/listCrawlers");
			}
		}
		// 编辑记录
		else {
			Crawler originCrawler = CmiHibernateProxyUtil.deproxy(crawlerDAO.findCrawlerByPrimaryKey(crawler.getId()));
			if (!crawler.equals(originCrawler) && crawler.equals(existingCrawler)) {
				mav.addAllObjects(crawlerService.loadCrawlerClassReferencedSet(crawler));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawler/editCrawler.jsp");
			} else {
				crawlerService.saveCrawler(crawler);
				mav.setViewName("forward:/listCrawlers");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
